package com.ucs.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
//import android.util.Log;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.ucs.domain.Order;
import com.ucs.util.UCSData;
import com.ucs.util.UCSUtil;

public class ReviewOpenOrderActivity extends SimpleActivity 
	implements AdapterView.OnItemClickListener, SimpleActivity.ResultCallbackIF{
	private OrderListAdaptor openOrderListAdaptor;
	private List<Order> openOrders;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d("ucs", "+ReviewOpenOrderActivity.onCreate()");
		setContentView(R.layout.review_pending_orders);
		ListView order_listview = (ListView)findViewById(R.id.orders_list);
		setHeader(order_listview);
				
		openOrders = UCSUtil.getOpenOrderList();
		if (openOrders == null){
			openOrders = new ArrayList<Order>();
		}
		openOrderListAdaptor = new OrderListAdaptor(this, openOrders);
		order_listview.setAdapter(openOrderListAdaptor);
		order_listview.setOnItemClickListener(this);
		Log.d("ucs", "-ReviewOpenOrderActivity.onCreate()");
		
	}

	private void setHeader(ListView order_listview) {
		LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View convertView = li.inflate(R.layout.order_row, null);
		convertView.setBackgroundColor(Color.BLUE);
    	TextView ticket_num = (TextView)convertView.findViewById(R.id.ticket_num);
    	TextView table_id = (TextView)convertView.findViewById(R.id.table_id);
    	TextView table_desc = (TextView)convertView.findViewById(R.id.table_desc);

    	ticket_num.setText("Ticket");
    	table_id.setText("Table");
    	table_desc.setText("Total Items");
    	order_listview.addHeaderView(convertView, null, false);
    	order_listview.setHeaderDividersEnabled(true);
	}

	int	position = -1;
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Bundle extras = new Bundle();
		position = arg2-1;
		Order openOrder = openOrders.get(position);
		extras.putSerializable(NewOrderActivity.PENDING_ORDER, openOrder);
		launchSubActivity(NewOrderActivity.class, this, extras);
	}

	@Override
	public void resultCancel(Intent intent) {
		Log.d("ucs", "+ReviewOpenOrderActivity.resultCancel");
		Order modifiedOrder = NewOrderActivity.getCreatedOrder(intent);
		if (modifiedOrder != null){
			UCSData.getInstance().addPendingOrder(modifiedOrder);
			openOrders.remove(position);
			openOrderListAdaptor.notifyDataSetChanged();
		}
		Log.d("ucs", "-ReviewOpenOrderActivity.resultCancel");				
	}

	@Override
	public void resultOk(Intent intent) {
		Log.d("ucs", "+ReviewOpenOrderActivity.resultOk");
		Order modifiedOrder = NewOrderActivity.getCreatedOrder(intent);
		openOrders.set(position, modifiedOrder);
		openOrderListAdaptor.notifyDataSetChanged();
		Log.d("ucs", "-ReviewOpenOrderActivity.resultOk");				
	}


}
